import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

/// 我的页面（个人信息页面）
/// 展示个人信息、GitCode账号、CSDN地址等
class ProfilePage extends StatelessWidget {
  const ProfilePage({super.key});

  /// 个人信息数据
  static const String name = '懿轩';
  static const String gitcodeUsername = '@byyixuan';
  static const String gitcodeUrl = 'https://gitcode.com/byyixuan';
  static const String csdnUrl = 'https://blog.csdn.net/2301_80035882?spm=1000.2115.3001.5343';
  static const String bio = 
      '作为一名充满热情的全栈开发学习者，我专注于 Java、C++、Python 及华为鸿蒙（HarmonyOS）等技术栈的深入学习与实践。'
      '在不断提升自身技术能力的同时，我致力于系统性地整理和分享学习资料、实践经验与项目成果，力求在精进自我的过程中赋能他人。';
  
  /// 头像路径（使用本地资源，如果本地图片不存在则使用网络图片作为备用）
  static const String avatarUrl = 'assets/images/avatar.png';
  static const String fallbackAvatarUrl = 'https://gitcode.com/uploads/-/system/user/avatar/avatar.png';

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final muted = theme.colorScheme.onSurfaceVariant;
    
    return Scaffold(
      appBar: AppBar(
        title: const Text('我的'),
        elevation: 0,
      ),
      body: SingleChildScrollView(
        padding: const EdgeInsets.all(24),
        child: Column(
          children: [
            const SizedBox(height: 20),
            
            // 头像和姓名
            _buildAvatarSection(context, theme),
            const SizedBox(height: 32),
            
            // 个人信息卡片
            _buildInfoCard(
              context,
              theme,
              title: '个人信息',
              icon: Icons.person_outline,
              children: [
                _buildInfoItem(
                  context,
                  icon: Icons.person,
                  label: '姓名',
                  value: name,
                ),
                const Divider(),
                _buildInfoItem(
                  context,
                  icon: Icons.account_circle,
                  label: 'GitCode账号',
                  value: gitcodeUsername,
                ),
              ],
            ),
            const SizedBox(height: 16),
            
            // 链接卡片
            _buildInfoCard(
              context,
              theme,
              title: '相关链接',
              icon: Icons.link,
              children: [
                _buildLinkItem(
                  context,
                  icon: Icons.code,
                  label: 'GitCode地址',
                  value: gitcodeUrl,
                  onTap: () => _launchUrl(gitcodeUrl),
                ),
                const Divider(),
                _buildLinkItem(
                  context,
                  icon: Icons.article,
                  label: 'CSDN地址',
                  value: csdnUrl,
                  onTap: () => _launchUrl(csdnUrl),
                ),
              ],
            ),
            const SizedBox(height: 16),
            
            // 个人简介卡片
            _buildInfoCard(
              context,
              theme,
              title: '个人简介',
              icon: Icons.description_outlined,
              children: [
                Padding(
                  padding: const EdgeInsets.all(16),
                  child: Text(
                    bio,
                    style: theme.textTheme.bodyMedium?.copyWith(
                      height: 1.6,
                      color: muted,
                    ),
                  ),
                ),
              ],
            ),
            const SizedBox(height: 32),
          ],
        ),
      ),
    );
  }

  Widget _buildAvatarSection(BuildContext context, ThemeData theme) {
    final muted = theme.colorScheme.onSurfaceVariant;

    return Column(
      children: [
        // 头像
        Container(
          width: 120,
          height: 120,
          decoration: BoxDecoration(
            shape: BoxShape.circle,
            border: Border.all(
              color: theme.colorScheme.primary.withValues(alpha: 0.3),
              width: 3,
            ),
            boxShadow: [
              BoxShadow(
                color: theme.colorScheme.primary.withValues(alpha: 0.2),
                blurRadius: 20,
                offset: const Offset(0, 10),
              ),
            ],
          ),
          child: ClipOval(
            child: Image.asset(
              avatarUrl,
              fit: BoxFit.cover,
              errorBuilder: (context, error, stackTrace) {
                // 如果本地图片加载失败，尝试加载网络图片作为备用
                return Image.network(
                  fallbackAvatarUrl,
                  fit: BoxFit.cover,
                  errorBuilder: (context, error, stackTrace) {
                    // 如果网络图片也加载失败，显示默认头像图标
                    return Container(
                      color: theme.colorScheme.surfaceContainerHighest,
                      child: Icon(
                        Icons.person,
                        size: 60,
                        color: theme.colorScheme.onSurfaceVariant,
                      ),
                    );
                  },
                );
              },
            ),
          ),
        ),
        const SizedBox(height: 20),
        // 姓名
        Text(
          name,
          style: theme.textTheme.headlineSmall?.copyWith(
            fontWeight: FontWeight.bold,
          ),
        ),
        const SizedBox(height: 8),
        // GitCode账号
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Icon(
              Icons.code,
              size: 16,
              color: muted,
            ),
            const SizedBox(width: 4),
            Text(
              gitcodeUsername,
              style: theme.textTheme.bodyMedium?.copyWith(
                color: muted,
              ),
            ),
          ],
        ),
      ],
    );
  }

  Widget _buildInfoCard(
    BuildContext context,
    ThemeData theme, {
    required String title,
    required IconData icon,
    required List<Widget> children,
  }) {
    final borderColor = theme.colorScheme.outlineVariant.withValues(alpha: 0.5);

    return Card(
      elevation: 0,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(16),
        side: BorderSide(
          color: borderColor,
        ),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Padding(
            padding: const EdgeInsets.all(16),
            child: Row(
              children: [
                Icon(icon, color: theme.colorScheme.primary, size: 24),
                const SizedBox(width: 8),
                Text(
                  title,
                  style: theme.textTheme.titleMedium?.copyWith(
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ],
            ),
          ),
          ...children,
        ],
      ),
    );
  }

  Widget _buildInfoItem(
    BuildContext context, {
    required IconData icon,
    required String label,
    required String value,
  }) {
    final theme = Theme.of(context);
    final muted = theme.colorScheme.onSurfaceVariant;

    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
      child: Row(
        children: [
          Icon(icon, size: 20, color: muted),
          const SizedBox(width: 12),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  label,
                  style: TextStyle(
                    fontSize: 12,
                    color: muted,
                  ),
                ),
                const SizedBox(height: 4),
                Text(
                  value,
                  style: theme.textTheme.titleMedium?.copyWith(fontSize: 16),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildLinkItem(
    BuildContext context, {
    required IconData icon,
    required String label,
    required String value,
    required VoidCallback onTap,
  }) {
    final theme = Theme.of(context);
    final muted = theme.colorScheme.onSurfaceVariant;
    final linkColor = theme.colorScheme.primary;

    return InkWell(
      onTap: onTap,
      child: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
        child: Row(
          children: [
            Icon(icon, size: 20, color: muted),
            const SizedBox(width: 12),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    label,
                    style: TextStyle(
                      fontSize: 12,
                      color: muted,
                    ),
                  ),
                  const SizedBox(height: 4),
                  Text(
                    value,
                    style: TextStyle(
                      fontSize: 14,
                      color: linkColor,
                      decoration: TextDecoration.underline,
                    ),
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                  ),
                ],
              ),
            ),
            Icon(
              Icons.open_in_new,
              size: 18,
              color: muted.withValues(alpha: 0.6),
            ),
          ],
        ),
      ),
    );
  }

  /// 打开URL链接
  Future<void> _launchUrl(String url) async {
    final uri = Uri.parse(url);
    if (await canLaunchUrl(uri)) {
      await launchUrl(uri, mode: LaunchMode.externalApplication);
    } else {
      // 如果无法打开，可以显示错误提示
      debugPrint('无法打开链接: $url');
    }
  }
}

